/**
 * Copyright (c) 2015-2016, Michael Yang 杨福海 (fuhai999@gmail.com).
 *
 * Licensed under the GNU Lesser General Public License (LGPL) ,Version 3.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.gnu.org/licenses/lgpl-3.0.txt
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package io.jpress.model.query;

import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.ehcache.IDataLoader;
import io.jpress.model.HouseData;
import io.jpress.model.Metadata;
import io.jpress.model.HouseData;
import io.jpress.template.TemplateManager;
import io.jpress.template.TplModule;

import java.math.BigInteger;
import java.util.LinkedList;
import java.util.List;

public class HouseDataQuery extends JBaseQuery {
	protected static final HouseData DAO = new HouseData();
	private static final HouseDataQuery QUERY = new HouseDataQuery();

	public static HouseDataQuery me() {
		return QUERY;
	}

	public List<HouseData> findList(int page, int pagesize, String day, String orderBy) {
		StringBuilder sqlBuilder = new StringBuilder("select * from jpress_house_data u ");

		LinkedList<Object> params = new LinkedList<Object>();

		boolean needWhere = true;
		needWhere = appendIfNotEmpty(sqlBuilder, "u.day", day, params, needWhere);

		buildOrderBy(orderBy, sqlBuilder);

		sqlBuilder.append(" LIMIT ?, ?");
		params.add(page - 1);
		params.add(pagesize);

		if (params.isEmpty()) {
			return DAO.find(sqlBuilder.toString());
		} else {
			return DAO.find(sqlBuilder.toString(), params.toArray());
		}

	}

	public HouseData findFirstFromMetadata(String key, Object value) {
//		return DAO.findFirstFromMetadata(key, value);
		Metadata md = MetaDataQuery.me().findFirstByTypeAndValue(HouseData.METADATA_TYPE, key, value);
		if (md != null) {
			BigInteger id = md.getObjectId();
			return findById(id);
		}
		return null;
	}

	public Page<HouseData> paginate(int pageNumber, int pageSize , String orderby) {
		String select = "select * ";
		StringBuilder fromBuilder = new StringBuilder(" from jpress_house_data u ");
		buildOrderBy(orderby, fromBuilder);
		return DAO.paginate(pageNumber, pageSize, select, fromBuilder.toString());
	}

	public long findCount() {
		return DAO.doFindCount();
	}

	public HouseData findById(final BigInteger houseDataId) {
		return DAO.getCache(houseDataId, new IDataLoader() {
			@Override
			public Object load() {
				return DAO.findById(houseDataId);
			}
		});
	}

	protected static void buildOrderBy(String orderBy, StringBuilder fromBuilder) {
		if ("day".equals(orderBy)) {
			fromBuilder.append(" ORDER BY u.day DESC");
		}
		else if ("time".equals(orderBy)) {
			fromBuilder.append(" ORDER BY u.time ASC");
		}
	}

}
